<?php

include_once dirname(__FILE__). '/OController.php';

class AdvController extends OController {
	
	public function __construct($id, $module=null) {
		parent::__construct($id, $module);
		$this->menu = array(
			array('label' => 'Links & Codes Overview', 'url' => array('owner/adv/overview'))
		);
		foreach (Website::model()->findAll() as $k => $website) {
			$this->menu[] = array(
				'label' => str_ireplace("www.", "", $website['domain']),
				'url' => 'javascript:void(0)',
				'items' => array(
					array('label' => 'Text Links', 'url' => array("owner/adv/texts", "website" => $website['website_id'])),
					array('label' => 'Banners', 'url' => array("owner/adv/banners", "website" => $website['website_id'])),
					//array('label' => 'Co-brand', 'url' => array("owner/adv/cobrand", "website" => $website['website_id'])),
				)
			);
		}
	}
	
	public function actionOverview(){
		$this->render("overview");
	}
	
	public function actionTexts($website) {
		$advs = Adv::model()->findAll('website_id=:website_id', array(':website_id' => $website));
		rsort($advs);
		$website = Website::model()->findByPk($website);
		$this->render("texts", array("advs" => $advs, "website" => $website));
	}
	
	public function actionView($id) {
		$this->layout = "//layouts/";
		$adv = Adv::model()->findByPk($id);
		$adv->data = unserialize($adv->data);
		$this->render("view", array("adv" => $adv));
	}
	
	public function actionBanners($website) {
		$arr = array(
			"pagination" => array("pageSize" => "30"),
			"criteria" => array(
				"condition" => "website_id='$website' AND type='banner'",
				"order" => "adv_id DESC"
			)
		);
		$dataProvider = new CActiveDataProvider("Adv", $arr);
		$newData = array();
		foreach ($dataProvider->data as $k => $item) {
			$d = unserialize($item->data);
			if (!empty($item->data)) {
				if ($d['width'] > 500 && $d['width'] > $d['height']) {
					$d['img_size'] = "width=500";
				} else if ($d['height'] > 200 && $d['height'] > $data['width']) {
					$d['img_size'] = "height=200";
				}
				$item->data = $d;
			}
			$newData[$k] = $item;
		}
		$dataProvider->setData($newData);
		
		$website = Website::model()->findByPk($website);
		
		$this->render("banners", array("dataProvider" => $dataProvider, "website" => $website));
	}
}